package com.it.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.travel.dao.FavoriteDao;
import com.it.travel.pojo.Favorite;
import com.it.travel.pojo.Route;
import com.it.travel.pojo.User;
import com.it.travel.service.FavoriteService;
import com.it.travel.vo.PageBean;
import com.it.travel.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@Service("favoriteService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteDao favoriteDao;

    /**
     * 收藏回显功能
     * @param rid 查询详情路线id
     * @return
     */
    @Override
    public ResultInfo isFavoriteByRid(Integer rid) {
//        前端发送旅游路线ID到后台，后台从session下获取用户信息进行判断：
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        ServletRequestAttributes attributes=(ServletRequestAttributes)requestAttributes;
        User user = (User) attributes.getRequest().getSession().getAttribute("user");
        if (user == null) {
//        1.如果session下的用户信息未null，则返回false且携带用户未登录的状态信息；
            return new ResultInfo(false, "用户未登录，请先登录");
        }
//        2.如果用户存在，根据用户id和旅游路线id查询：
        Favorite favorite = favoriteDao.isFavoriteByRid(user.getUid(), rid);
        if (favorite == null) {
//        2.2 收藏不存在，则返回flag:true, data:false
            return new ResultInfo(true,false,null);
        }
//        2.1 收藏存在，则返回flag:true data:true
        return new ResultInfo(true, true,null);

    }


    /**
     * 我的收藏展示
     * @param curPage
     * @return
     */
    @Override
    public ResultInfo findFavoriteByPage(Integer curPage) {
        PageHelper.startPage(curPage,12);

        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        ServletRequestAttributes attributes=(ServletRequestAttributes)requestAttributes;

        User user = (User) attributes.getRequest().getSession().getAttribute("user");

        if (user == null) {
            new ResultInfo(false, "用户未登录");
        }

        List<Favorite> favoriteList = favoriteDao.findFavoriteByPage(user.getUid());

        PageInfo<Favorite> favoritePageInfo = new PageInfo<>(favoriteList);
        PageBean<Favorite> routePageBean = new PageBean<>();
        routePageBean.setTotalPage(favoritePageInfo.getPages());
        routePageBean.setPrePage(favoritePageInfo.getPrePage());
        routePageBean.setNextPage(favoritePageInfo.getNextPage());
        routePageBean.setFirstPage(favoritePageInfo.getNavigateFirstPage());
        routePageBean.setCurPage(favoritePageInfo.getPageNum());
        routePageBean.setData(favoritePageInfo.getList());
        routePageBean.setCount(favoritePageInfo.getTotal());

        return new ResultInfo(true,routePageBean,null);
    }
}
